<?php
/**
 * @version 	$Id: fController.class.php 84 2010-03-18 00:06:10Z mylifeisskidrow@gmail.com $
 * @author 		$Author: mylifeisskidrow $
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reserved.
 * @license 	GNU/GPL see license.php
 */

defined('IN_FLYWHEEL') or die('Restricted Access!');
/**
 * Base Controller
 * 
 * @abstract 
 * @package 	system
 * @subpackage 	controller
 *
 */
abstract class fController {	
	/**
	 * Render Mode
	 *
	 * @var string
	 */
	protected $_renderMode;
	
	protected $lang = array();
	protected $name;	
	
	protected $_view = 'default';
	
	public function __construct($name) {
		$this->name = $name;
		fFactory::getI18N()->getLanguageSource()->import($name, false);
	}
	
	/**
	 * Execute
	 * 
	 * @return component process result
	 *
	 */
	public function execute() {
		$action = 'execute' . ucfirst(fFactory::getRouter()->getAction());	
		
		if (!method_exists($this, $action)) {
			throw new fError404Exception("Action \"".fFactory::getRouter()->getController().'/'.$action ."\" doesn't exist");						
		}
		$this->$action();
				
		if ($this->_renderMode == null) { //Default render Component
			return $this->renderComponent();
		}
	}
	
	/**
	 * Render Partial
	 * 	only render a web page's partial. 
	 *
	 * @param string
	 */
	protected function renderPartial($vars = null) {
		$this->_renderMode = 'PARTIAL';
		$template = fFactory::getTemplate();
		
		// Neu goi trong cac child controller, gia tri nay se duoc set = false
		$view = 'controllers'._DS_.fFactory::getRouter()->getController()._DS_.$this->_view;
		$vars = (array) $vars;		
		$vars = ($vars === null) ? get_object_vars($this) : array_merge(get_object_vars($this), $vars);				
				
		foreach ($vars as $key=>$value) {
			$template->assign($key, $value);
		}		
		$template->assign('lang', fFactory::getI18N());				
		$buffer = $template->render($view);
		//$template->clearAllAssign();
		return $buffer;
	}
	
	/**
	 * Render Component
	 * 	render web page
	 *
	 * @return string
	 */
	protected function renderComponent() {
		$this->loadModules();
		$buffer = $this->renderPartial();		
		$this->_renderMode = 'COMPONENT';
		return $buffer;
	}
	
	/**
	 * Render Text
	 *
	 * @param string $text
	 * @return string
	 */
	protected function renderText($text) {
		$this->_renderMode = 'TEXT';
		return $text;	
	}
	
	/**
	 * Set Layout
	 * 	set layout template
	 *
	 * @param string $layout
	 */
	public function setLayout($layout) {
		$application = fFactory::getApplication();
		if ($application instanceof fWebApplication) {
			$application->setLayout($layout);
		}
	}
	
	/**
	 * Get current layout template
	 * 
	 * @return mixed: string|null
	 *
	 */
	public function getLayout() {
		$application = fFactory::getApplication();
		if ($application instanceof fWebApplication ) {
			return $application->getLayout();				
		}
		
		return null;
	}
	
	/**
	 * Set view
	 *
	 * @param string $view
	 */
	protected function setView($view) {
		$this->_view = $view;		
	}
	
	abstract public function executeDefault();
	
	/**
	 * Get Render Mode	 
	 *
	 * @return string
	 */
	final public function getRenderMode() {
		return $this->_renderMode;
	}
	
	protected function loadModules() {		
	}
	
	/**
	 * Load Language
	 *
	 * @param string $namespace
	 * @param boolean $require
	 * 
	 * @return void
	 * @throws fException if $require = true and not found language namespace
	 */
	protected function loadLanguage($namespace, $require = true) {
		fFactory::getI18N()->getLanguageSource()->import($namespace, $require);			
	}
}